using UnityEngine;

public class OutlineController : MonoBehaviour
{
    private Camera mainCamera;
    private GameObject lastHoverObject;
    private GameObject lockedObject;
    private float lastClickTime;
    private float doubleClickThreshold = 0.3f;

    [Header("渲染层级设置")]
    public int hoverLayer = 1;
    public int clickLayer = 2;
    public int doubleClickLayer = 3;

    void Start()
    {
        mainCamera = Camera.main;
        if (mainCamera == null)
        {
            mainCamera = FindObjectOfType<Camera>();
        }
    }

    void Update()
    {
        HandleMouseInput();
    }

    private void HandleMouseInput()
    {
        Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        // 处理鼠标悬停
        if (Physics.Raycast(ray, out hit))
        {
            GameObject currentObject = hit.collider.gameObject;
            
            // 如果有锁定对象，忽略悬停其他对象
            if (lockedObject != null)
            {
                // 只处理锁定对象的点击事件
                if (Input.GetMouseButtonDown(0) && currentObject == lockedObject)
                {
                    HandleDoubleClick(currentObject);
                }
                return;
            }

            // 正常悬停逻辑
            if (currentObject != lastHoverObject)
            {
                // 重置上一个悬停对象的层级
                if (lastHoverObject != null)
                {
                    ResetObjectLayer(lastHoverObject);
                }

                // 设置新悬停对象的层级
                SetObjectLayer(currentObject, hoverLayer);
                lastHoverObject = currentObject;
            }

            // 处理鼠标点击
            if (Input.GetMouseButtonDown(0))
            {
                HandleDoubleClick(currentObject);
            }
        }
        else
        {
            // 鼠标不在任何对象上，重置悬停状态（仅当没有锁定对象时）
            if (lockedObject == null && lastHoverObject != null)
            {
                ResetObjectLayer(lastHoverObject);
                lastHoverObject = null;
            }
        }
    }

    private void HandleDoubleClick(GameObject currentObject)
    {
        float timeSinceLastClick = Time.time - lastClickTime;
        
        if (timeSinceLastClick <= doubleClickThreshold)
        {
            if (lockedObject == currentObject)
            {
                // 再次双击锁定对象，解除锁定
                ResetObjectLayer(lockedObject);
                lockedObject = null;
                
                // 立即设置悬停效果
                SetObjectLayer(currentObject, hoverLayer);
                lastHoverObject = currentObject;
            }
            else
            {
                // 双击新对象，锁定该对象
                if (lockedObject != null)
                {
                    ResetObjectLayer(lockedObject);
                }
                
                lockedObject = currentObject;
                SetObjectLayer(currentObject, doubleClickLayer);
                
                // 清除悬停状态
                if (lastHoverObject != null && lastHoverObject != currentObject)
                {
                    ResetObjectLayer(lastHoverObject);
                }
                lastHoverObject = null;
            }
        }
        else
        {
            // 单击（仅当没有锁定时生效）
            if (lockedObject == null)
            {
                SetObjectLayer(currentObject, clickLayer);
            }
        }
        
        lastClickTime = Time.time;
    }

    private void SetObjectLayer(GameObject obj, int layer)
    {
        Renderer renderer = obj.GetComponent<Renderer>();
        if (renderer != null)
        {
            renderer.renderingLayerMask = (uint)(1 << layer);
        }
    }

    private void ResetObjectLayer(GameObject obj)
    {
        // 重置为默认层级（0）
        Renderer renderer = obj.GetComponent<Renderer>();
        if (renderer != null)
        {
            renderer.renderingLayerMask = 1; // 默认层级
        }
    }
}